package top.idcard.agent.biz.rw.channel;

import top.idcard.agent.biz.entity.Certification;
import top.idcard.agent.biz.entity.NativeIDCard;
import top.idcard.agent.biz.util.ByteUtil;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharsetDecoder;

/**
 * @author wx
 * @version 1.0
 * @date 2021/2/20 3:06 下午
 */
public class CertificationByteDataTool {

    public static Certification getFromBytes(byte[] data){
        Certification.CertificationEnum certificationEnum = Certification.CertificationEnum.valueOf(Integer.valueOf(Character.valueOf((char) data[0]).toString()));
        Certification certification = null;
        switch (certificationEnum){
            case Native:
                certification = getNativeIDCardFromBytes(data);
                break;
            case Hong:
                break;
            case Foreign:break;
        }
        return certification;
    }

    public static NativeIDCard getNativeIDCardFromBytes(byte[] data){
        NativeIDCard nativeIDCard = new NativeIDCard();
        int offset = 1;
        nativeIDCard.setName(ByteUtil.byteToString(data,offset, 30));
        offset += 30;

        nativeIDCard.setSex(ByteUtil.byteToShort(data, offset));
        offset += 2;

        nativeIDCard.setNation(ByteUtil.byteToString(data, offset, 20));
        offset += 20;

        nativeIDCard.setBirthday(ByteUtil.byteToString(data, offset, 16));
        offset+= 16;

        nativeIDCard.setAddr(ByteUtil.byteToString(data, offset, 70));
        offset += 70;

        nativeIDCard.setNo(ByteUtil.byteToString(data, offset, 36));
        offset += 36;

        nativeIDCard.setSignOrganization(ByteUtil.byteToString(data, offset, 30));
        offset += 30;

        nativeIDCard.setValidationStart(ByteUtil.byteToString(data, offset, 16));
        offset += 16;

        nativeIDCard.setValidationEnd(ByteUtil.byteToString(data, offset, 16));
        offset += 16;

        nativeIDCard.setImg(ByteUtil.subByteArray(data, offset, 38862));
        offset += 38862;

        nativeIDCard.setFingerprint(ByteUtil.subByteArray(data, offset, 1024));
        offset += 1024;
        return nativeIDCard;
    }
}
